package com.hydrografix.android.ttrack.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.hydrografix.android.ttrack.R;

public class DatabaseInstance {
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private Context mCtx;
	private static DatabaseInstance currentInstance;
		
	public static void initialize(Context ctx) {
		if(currentInstance == null) {
			DatabaseInstance instance = new DatabaseInstance();
			currentInstance = instance; 
			instance.mCtx = ctx;			
		}
	}
	
	private void openInstance() {
		mDbHelper = new DatabaseHelper(mCtx, mCtx.getString(R.string.database_name));
		mDb = mDbHelper.getWritableDatabase();
//		mDb.execSQL("DROP TABLE IF EXISTS " + DatabaseHelper.TIME_SLICE_CATEGORY_TABLE);
//		mDb.execSQL("DROP TABLE IF EXISTS " + DatabaseHelper.TIME_SLICE_TABLE);
//		mDb.execSQL("CREATE TABLE " + DatabaseHelper.TIME_SLICE_CATEGORY_TABLE
//				+ " (_id integer primary key autoincrement, "
//				+ "category_name text, description text)");
//		mDb.execSQL("CREATE TABLE " + DatabaseHelper.TIME_SLICE_TABLE + " (_id integer primary key autoincrement, "
//						+ "category_id integer, start_time integer, end_time integer)");

	}
	
	public static void open() {
		if(DatabaseInstance.getDb() == null || !DatabaseInstance.getDb().isOpen()) {
			DatabaseInstance.currentInstance.openInstance();
		}
	}
	
	public static void close() {
		currentInstance.mDb.close();
		currentInstance.mDbHelper.close();
	}

	public static SQLiteDatabase getDb() {
		if(currentInstance.mDb != null && !currentInstance.mDb.isOpen()) {
			currentInstance.openInstance();
		}
		return currentInstance.mDb;
	}
	
	
}
